// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Service.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.LiveObjects
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Org.OpenTrader.Framework.Enums;
    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.LiveObjects.Interfaces;

    #endregion

    /// <summary>
    /// Basic Service
    /// </summary>
    public abstract class Service : LiveObject, IService
    {
        #region Constants and Fields

        /// <summary>
        /// The pluginsMap.
        /// </summary>
        private readonly IDictionary<Guid, IPlugin> pluginsMap = new Dictionary<Guid, IPlugin>();

        /// <summary>
        /// The plugins.
        /// </summary>
        private IPlugin[] plugins = new IPlugin[] { };

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Service"/> class.
        /// </summary>
        /// <param name="descriptor">
        /// The descriptor.
        /// </param>
        /// <param name="container">
        /// The container.
        /// </param>
        public Service(ILiveObjectDescriptor descriptor, ILiveObjectContainer container)
            : base(descriptor, container)
        {
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets Plugins.
        /// </summary>
        public IPlugin[] Plugins
        {
            get
            {
                return this.plugins;
            }
        }

        #endregion

        #region Indexers

        /// <summary>
        /// The this.
        /// </summary>
        /// <param name="pluginId">
        /// The pluginId.
        /// </param>
        public IPlugin this[Guid pluginId]
        {
            get
            {
                if (this.pluginsMap.ContainsKey(pluginId))
                {
                    return this.pluginsMap[pluginId];
                }

                return null;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The activate.
        /// </summary>
        /// <param name="application">
        /// The application.
        /// </param>
        /// <param name="service">
        /// The service.
        /// </param>
        public void Activate(IApplication application, IService service)
        {
            // Change Current Directory for this domain
            Environment.CurrentDirectory = service.Container.Descriptor.WorkingDirectory;

            this.LiveObjectsEventCB.RaiseEvent(this, new LiveObjectsEventArgs("Activating " + service.Descriptor.Name));

            // Activate all services
            foreach (var plugin in service.Plugins)
            {
                plugin.LiveObjectsEventCB.Chain(this.LiveObjectsEventCB);
                (plugin as Plugin).Activate(service, plugin);
            }

            // Post activation, event to service
            service.OnActivation(
                AppDomain.CurrentDomain, new ActivationEventArgs.CreateDestroy(EActivationType.Activation));

            // Post service activation to owner application
            application.OnActivation(
                AppDomain.CurrentDomain, 
                new ActivationEventArgs.OwnedServiceCreateDestroy(EActivationType.Activation, service));

            this.LiveObjectsEventCB.RaiseEvent(this, new LiveObjectsEventArgs("Activated " + service.Descriptor.Name));
        }

        /// <summary>
        /// The add plugin.
        /// </summary>
        /// <param name="p">
        /// The p.
        /// </param>
        public void AddPlugin(IPlugin p)
        {
            this.pluginsMap.Add(p.Descriptor.ID, p);
            this.plugins = this.pluginsMap.Values.ToArray();
        }

        /// <summary>
        /// The desactivate.
        /// </summary>
        /// <param name="application">
        /// The application.
        /// </param>
        /// <param name="service">
        /// The service.
        /// </param>
        public void Desactivate(IApplication application, IService service)
        {
            // Change Current Directory for this domain
            Environment.CurrentDirectory = service.Container.Descriptor.WorkingDirectory;

            this.LiveObjectsEventCB.RaiseEvent(
                this, new LiveObjectsEventArgs("Desactivating " + service.Descriptor.Name));

            // Desctivate all plugins
            foreach (var plugin in service.Plugins)
            {
                (plugin as Plugin).Desactivate(service, plugin);
                plugin.LiveObjectsEventCB.UnChain(this.LiveObjectsEventCB);
            }

            // Post deactivation, event to service
            service.OnActivation(
                AppDomain.CurrentDomain, new ActivationEventArgs.CreateDestroy(EActivationType.Desactivation));

            // Post service deactivation to owner application
            application.OnActivation(
                AppDomain.CurrentDomain, 
                new ActivationEventArgs.OwnedServiceCreateDestroy(EActivationType.Desactivation, service));

            this.LiveObjectsEventCB.RaiseEvent(this, new LiveObjectsEventArgs("Desactivated " + service.Descriptor.Name));
        }

        #endregion
    }
}